package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.ProjectCode;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.vo.ProjectStatisticsCountVO;
import com.zygh.policeasset.service.ProcessRecordSerivce;
import com.zygh.policeasset.service.ProjectNoService;
import com.zygh.policeasset.service.ProjectService;
import com.zygh.policeasset.service.UploadFileService;
import com.zygh.policeasset.utils.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.tools.ant.taskdefs.optional.ccm.CCMReconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping({CCMReconfigure.FLAG_PROJECT})
@Api(tags = {"立项管理模块"})
@RestController
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectNoService projectNoService;
    @Autowired
    ProjectCode projectCode;
    @Autowired
    UploadFileService uploadFileService;
    @Autowired
    ProcessRecordSerivce processRecordSerivce;
    @Autowired
    EasyExcelUtil easyExcelUtil;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部立项列表")
    public Object getAll() {
        return this.projectService.findAll();
    }

    @RequestMapping(value = {"/selectAll"}, method = {RequestMethod.GET})
    @ApiOperation("获取项目列表")
    public Object selectAll(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.projectService.selectAll(pageNum, pageSize);
    }

    @RequestMapping(value = {"/root"}, method = {RequestMethod.GET})
    @ApiOperation("获取上级立项分页列表")
    public Object getAllByRoot(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.projectService.findAllByRoot(pageNum, pageSize);
    }

    @RequestMapping(value = {"/selectBytype"}, method = {RequestMethod.GET})
    @ApiOperation("查询所有通过审核的立项项目")
    public Object selectBytype() {
        return this.projectService.selectBytype();
    }

    @RequestMapping(value = {"/root/list"}, method = {RequestMethod.GET})
    @ApiOperation("获取上级立项下拉列表")
    public Object getAllByRootList() {
        return this.projectService.findAllByRootList();
    }

    @RequestMapping(value = {"/child"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "pid", value = "pid上级ID", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("获取本级立项分页列表")
    public Object getChildByRoot(@RequestParam int pid,
                                 @RequestParam(value = "status", required = false) Integer status,
                                 @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                 @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                                 @RequestParam(required = false) String name,
                                 @RequestParam(required = false) String time1,
                                 @RequestParam(required = false) String time2,
                                 @RequestParam(required = false) String projectNo,
                                 @RequestHeader("findAll") boolean findAll,
                                 @RequestHeader("userId") Integer userId) {
        if (StrUtil.isEmpty(String.valueOf(pid))) {
            return new CommonResult().validateFailed("pid不能为空");
        }
        if (findAll) {
            return this.projectService.findChildByRoot(pid, status, pageNum, pageSize, name, time1, time2, projectNo, null);
        }
        return this.projectService.findChildByRoot(pid, status, pageNum, pageSize, name, time1, time2, projectNo, userId);
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0未提交  2已提交", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取立项分页列表")
    public Object getByStatus(@RequestParam(defaultValue = "0", value = "category") int category, @RequestParam(defaultValue = "", value = "name") String name, @RequestParam(defaultValue = "", value = "startTime") String startTime, @RequestParam(defaultValue = "", value = "endTime") String endTime, @RequestParam(defaultValue = "0", value = "status") int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(category))) {
            return new CommonResult().validateFailed("类型不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.projectService.findByStatus(category, name, status, startTime, endTime, pageNum, pageSize);
    }

    @RequestMapping(value = {"/search"}, method = {RequestMethod.GET})
    @ApiOperation("立项搜索")
    public Object search(@RequestParam int category, @RequestParam(defaultValue = "0", value = "status") Integer status, @RequestParam(defaultValue = "0", value = "userId") Integer userId, @RequestParam String name, String commissionNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(required = false) String time1, @RequestParam(required = false) String time2) {
        return this.projectService.search(category, status.intValue(), name, commissionNo, pageNum, pageSize, time1, time2, userId);
    }

    @RequestMapping(value = {"/findSuperiorProject"}, method = {RequestMethod.GET})
    @ApiOperation("查询上级项目")
    public Object findSuperiorProject(@RequestParam int category, @RequestParam(defaultValue = "0", value = "status") Integer status, @RequestParam(defaultValue = "0", value = "userId") Integer userId, @RequestParam(required = false) String name, @RequestParam(required = false) String commissionNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(required = false) String time1, @RequestParam(required = false) String time2) {
        return this.projectService.search(category, status.intValue(), name, commissionNo, pageNum, pageSize, time1, time2, userId);
    }

    @RequestMapping(value = {"/insert"}, method = {RequestMethod.POST})
    @ApiOperation("添加立项")
    public Object insert(@RequestBody Project params) {
        if (StrUtil.isEmpty(params.getName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (this.projectService.getAdd(params) != null) {
            return new CommonResult().validateFailed("项目名称不能重复");
        }
        if (this.projectService.add(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改立项")
    public Object update(@RequestBody Project params) {
        if (StrUtil.isEmpty(params.getName())) {
            return new CommonResult().validateFailed("名称不能为空");
        }
        if (this.projectService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/attachment"}, method = {RequestMethod.GET})
    @ApiOperation("获取立项附件（废弃）")
    public Object attachment(@RequestBody Project params) {
        if (StrUtil.isEmpty(params.getName())) {
            return new CommonResult().validateFailed("名称不能为空");
        }
        if (this.projectService.add(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除立项")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.projectService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/findbyCompleted"}, method = {RequestMethod.GET})
    @ApiOperation("获取竣工验收后可录资产列表（新）")
    public Object findbyCompleted(@RequestParam(defaultValue = "0", value = "companyId") String companyId, @RequestParam(defaultValue = "", value = "name") String name, @RequestParam String status, String commissionNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.projectService.findbyCompleted(companyId, name, status, commissionNo, pageNum, pageSize);
    }

    @RequestMapping(value = {"/findbycompany"}, method = {RequestMethod.GET})
    @ApiOperation("审计结算可录资产列表（新）")
    public Object findbySettlement(@RequestParam(value = "companyId", required = false) String companyId, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "dataLabelsId", required = false) Integer dataLabelsId, @RequestParam(value = "status", required = false) String status, @RequestParam(value = "commissionNo", required = false) String commissionNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(value = "company", required = false) String company, @RequestHeader(value = "findAll", required = false) boolean findAll, @RequestHeader(value = "userId", required = false) Integer userId, @RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "combined", required = false) Double combined) {
        if (findAll) {
            return this.projectService.findbySettlement(companyId, name, status, commissionNo, pageNum, pageSize, dataLabelsId, null, company, id, combined);
        }
        return this.projectService.findbySettlement(companyId, name, status, commissionNo, pageNum, pageSize, dataLabelsId, userId, company, id, combined);
    }

    @RequestMapping(value = {"/findbySettlementById"}, method = {RequestMethod.GET})
    @ApiOperation("审计结算可录资产列表（新）通过id查询")
    public Object findbySettlementById(@RequestParam(value = "id", required = false) Integer id) {
        return this.projectService.findbySettlementById(id);
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("根据id查询")
    public Object findById(@RequestParam(value = "id", required = false) Integer id) {
        return this.projectService.findById(id.intValue());
    }

    @RequestMapping(value = {"/findByCompanyExcel"}, method = {RequestMethod.GET})
    @ApiOperation("审计结算可录资产列表（新）导出")
    public Object findByCompanyExcel(@RequestParam(value = "companyId", required = false) String companyId, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "dataLabelsId", required = false) Integer dataLabelsId, @RequestParam String status, String commissionNo, HttpServletResponse response, @RequestParam(value = "userId", required = false) Integer userId, @RequestParam(value = "company", required = false) String company, @RequestHeader(value = "findAll", required = false) boolean findAll, @RequestHeader(value = "unitId", required = false) Integer unitId, @RequestParam(value = "id", required = false) Integer id) {
        if (findAll) {
            return this.projectService.findByCompanyExcel(companyId, name, status, commissionNo, dataLabelsId, response, userId, company, id);
        }
        return this.projectService.findByCompanyExcel(unitId.toString(), name, status, commissionNo, dataLabelsId, response, userId, company, id);
    }

    @RequestMapping(value = {"/affirmpay"}, method = {RequestMethod.POST})
    @ApiOperation("确认付款")
    public Object affirmPay(@RequestBody JSONObject jsonObject) {
        String type = jsonObject.get("type").toString();
        int id = Integer.parseInt(jsonObject.get("id").toString());
        String paymentTime = jsonObject.get("paymentTime").toString();
        if (StrUtil.isEmpty(type)) {
            return new CommonResult().validateFailed("类型不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (StrUtil.isEmpty(paymentTime)) {
            return new CommonResult().validateFailed("付款日期不能为空");
        }
        if (this.projectService.affirmPay(type, id, paymentTime).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("付款失败");
    }

    @RequestMapping(value = {"/getpay"}, method = {RequestMethod.GET})
    @ApiOperation("获取已付款立项列表")
    public Object getPay(@RequestParam String name, String commissionNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.projectService.getPay(name, commissionNo, pageNum, pageSize);
    }

    @RequestMapping(value = {"/findAllProjcetName"}, method = {RequestMethod.GET})
    @ApiOperation("获取项目名称")
    public Object findAllProjcetName(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.projectService.findAllProjcetName(pageNum, pageSize);
    }

    @RequestMapping(value = {"/findAllProjcetNameByid"}, method = {RequestMethod.GET})
    @ApiOperation("获取单条项目名称")
    public Object findAllProjcetNameByid(@RequestParam("id") int id) {
        return this.projectService.findAllProjcetNameByid(id);
    }

    @RequestMapping(value = {"/updateProjcetNameByid"}, method = {RequestMethod.GET})
    @ApiOperation("修改项目名称")
    public Object updateProjcetNameByid(@RequestParam("name") String name, @RequestParam("id") int id, @RequestParam("projectNo") String projectNo) {
        if (this.projectService.updateProjcetNameByid(name, id, projectNo) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败");
    }

    @RequestMapping(value = {"/deleteProjcetNameByid"}, method = {RequestMethod.GET})
    @ApiOperation("删除项目名称")
    public Object deleteProjcetNameByid(@RequestParam("id") int id) {
        if (this.projectService.deleteProjcetNameByid(id) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/selectProject"}, method = {RequestMethod.GET})
    @ApiOperation("项目管理全流程查询")
    public Object selectProject(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                                @RequestParam(value = "projectNo", required = false) String projectNo,
                                @RequestParam(value = "sectionId", required = false) Integer sectionId,
                                @RequestParam(value = "type", required = false) String type)
            throws InvocationTargetException, IllegalAccessException {
        return this.projectService.selectProject(pageNum, pageSize, projectNo, sectionId, type);
    }

    @RequestMapping(value = {"/selectProjectById"}, method = {RequestMethod.GET})
    @ApiOperation("项目管理全流程查询根据各类型id查询")
    public Object selectProjectById(@RequestParam(value = "id", required = false) Integer id,
                                    @RequestParam(value = "type", required = false) String type) {
        return this.projectService.selectProjectById(id, type);
    }

    @GetMapping(value = {"/projectStatisticsCount"})
    @ApiOperation("项目管理数量统计")
    public CommonResult projectStatisticsCount(@RequestParam(value = "status",required = false) Integer status,
                                               @RequestParam(value = "time1",required = false) String time1,
                                               @RequestParam(value = "time2",required = false) String time2) {
        return new CommonResult().success(projectService.projectStatisticsCount(status, time1, time2));
    }

    @GetMapping(value = {"/findPidByCategory"})
    @ApiOperation("根据类别查询上级信息")
    public CommonResult findPidByCategory(@RequestParam(value = "type") String type) {
        return new CommonResult().success(projectService.findPidByCategory(type));
    }
}